import {React,useEffect,useState} from 'react';
import { Breadcrumb, Row, Col, Form, Input, Select, DatePicker, Button ,message} from 'antd'
import { Link } from 'react-router-dom'
import FormItem from 'antd/lib/form/FormItem';
import axios from 'axios';

const { RangePicker } = DatePicker
const XiuGaiYiZhu = (props) => {
    // console.log(props);
    const [kesOption,setkesOption] = useState()
    const [lskesOption,setlskesOption]=useState()
    const [ysOption,setysOption] = useState()
    const [lsysOption,setlsysOption]=useState()
    const [changqiKeShi,setchangqiKeShi]=useState()
    const [lingshiKeShi,setlingshiKeShi]=useState()
    const [changqiYiSheng,setchangqiYiSheng]=useState()
    const [lingshiYiSheng,setlingshiYiSheng]=useState()
    const [chuyuanfou,setchuyuanfou] = useState([
        {
            key: 1,
            value: "已出院"
        },
        {
            key: 0,
            value: "未出院"
        }
    ])
    const [lingshi]=Form.useForm()
    const [changqi]=Form.useForm()
    useEffect(()=>{
        changqi.setFieldsValue({zhuYuanHao:props.location.state})
        lingshi.setFieldsValue({zhuYuanHao:props.location.state})
        getKeShi()
    },[])
    // 日期格式转换
    const returnTime=(time)=>{
        let Time=new Date(time)
        let year=Time.getFullYear()
        let month=Time.getMonth()+1
        month=month>9?month:"0"+month
        let day=Time.getDate()
        day=day>9?day:"0"+day
        let h=Time.getHours()
        h=h>9?h:"0"+h
        let m=Time.getMinutes()
        m=m>9?m:"0"+m
        let s=Time.getSeconds()
        s=s>9?s:"0"+s
        return year+"-"+month+"-"+day+" "+h+":"+m+":"+s
    }
    const lingshiYiZhu = (value) => {
        // console.log(lingshi);
        // console.log(value.yiZhuRiQi._d);
        let yiZhuRiQi=returnTime(new Date(value.yiZhuRiQi._d))
        let tempObj={
            chuYuanFou:value.chuYuanFou=value.chuYuanFou=="已出院"?1:0,
            keShiDaiMa:lingshiKeShi,
            yaoPinMingCheng:value.yaoPinMingCheng,
            yaoPinYongFa:value.yaoPinYongFa,
            yaoPinYongLiang:value.yaoPinYongLiang,
            yiShengBianMa:lingshiYiSheng,
            yiZhuRiQi:yiZhuRiQi,
            zhuYuanHao:value.zhuYuanHao
        }
        axios("/linShiYiZhuDengJiBiao/gengXinByZhuYuanHao",{
            method:"post",
            data:tempObj
        })
        .then(res=>{
            console.log(res);
            if(res.data.code==200){
                message.success("更新临时医嘱成功")
            }
        })
     }
    const changqiYiZhu = (value) => { 
        let kaiShiShiJian=returnTime(new Date(value.time[0]._d))
        let jieShuShiJian=returnTime(new Date(value.time[1]._d))
        let tempObj={
            chuYuanFou:value.chuYuanFou=value.chuYuanFou=="已出院"?1:0,
            kaiShiShiJian:kaiShiShiJian,
            keShiDaiMa:changqiKeShi,
            tingZhiShiJian:jieShuShiJian,
            yaoPinMingCheng:value.yaoPinMingCheng,
            yaoPinYongFa:value.yaoPinYongFa,
            yaoPinYongLiang:value.yaoPinYongLiang,
            yiShengBianMa:changqiYiSheng,
            yongYaoShiJian:value.yongYaoShiJian,
            zhuYuanHao:value.zhuYuanHao
        }
        // console.log(tempObj);
        axios("/changQiYiZhuDengJi/gengXinByZhuYuanHao",{
            method:"post",
            data:tempObj
        })
        .then(res=>{
            console.log(res);
            if(res.data.code==200){
                message.success("更新长期医嘱成功")
            }
        })
    }
    const goback = () => {
        props.history.go(-1)
    }
    // 获取科室信息
    const getKeShi=()=>{
        axios("/menZhenKeMuBiao/tongJiKeShi",{
            method:"post"
        })
        .then(res=>{
            console.log(res);
            if(res.data.code==200){
                let temp=res.data.menZhenKeMuBiaoList
                let arr=[]
                for(let i=0;i<temp.length;i++){
                    let tempObj={}
                    tempObj.key=temp[i].menZhenKeMuDaiHao
                    tempObj.value=temp[i].menZhenKeMuMingCheng
                    arr.push(tempObj)
                }
                console.log(arr);
                setkesOption(arr)
                setlskesOption(arr)
            }
        })
    }
    // 长期医嘱科室
    const cqKeShj=(key,value)=>{
        if(value){
            setchangqiKeShi(value.key)
            console.log(value.key);
        }else{
            setchangqiKeShi()
        }
        axios("/menZhenKeMuBiao/tongJiYiSheng",{
            method:"post",
            params:{menZhenKeMuDaiHao:value.key}
        })
        .then(res=>{
            console.log(res);
            if(res.data.code==200){
                let temp=res.data.menZhenKeMuBiaoList[0].yiShengSuoShuKeShiList
                let arr=[]
                for(let i=0;i<temp.length;i++){
                    let tempObj={}
                    tempObj.key=temp[i].yiShengDaiHao
                    tempObj.value=temp[i].yiShengMingCheng
                    arr.push(tempObj)
                }
                setysOption(arr)
            }
        })
    }
    //临时医嘱科室
    const lsKeShi=(key,value)=>{
        if(value){
            setlingshiKeShi(value.key)
            // console.log(value.key);
        }else{
            setlingshiKeShi()
        }
        axios("/menZhenKeMuBiao/tongJiYiSheng",{
            method:"post",
            params:{menZhenKeMuDaiHao:value.key}
        })
        .then(res=>{
            console.log(res);
            if(res.data.code==200){
                let temp=res.data.menZhenKeMuBiaoList[0].yiShengSuoShuKeShiList
                let arr=[]
                for(let i=0;i<temp.length;i++){
                    let tempObj={}
                    tempObj.key=temp[i].yiShengDaiHao
                    tempObj.value=temp[i].yiShengMingCheng
                    arr.push(tempObj)
                }
                setlsysOption(arr)
            }
        })
    }
    // 长期医嘱医生
    const cqYiSheng=(key,value)=>{
        if(value){
            setchangqiYiSheng(value.key)
            console.log(value.key);
        }else{
            setchangqiYiSheng()
        }
    }
    // 临时医嘱医生
    const lsYiSheng=(key,value)=>{
        if(value){
            setlingshiYiSheng(value.key)
            console.log(value.key);
        }else{
            setlingshiYiSheng()
        }
    }
    return (
        <div>
            {/* 面包屑导航 */}
            <Row>
                <Col>
                    <Breadcrumb style={{ marginBottom: 24 }}>
                        <Breadcrumb.Item>病房管理</Breadcrumb.Item>
                        <Breadcrumb.Item>
                            <Link to={{ pathname: "/zy/xiugaibingchuang" }}>修改病床</Link>
                        </Breadcrumb.Item>
                        <Breadcrumb.Item>修改病床信息</Breadcrumb.Item>
                    </Breadcrumb>
                </Col>
            </Row>
            <Row>
                <Col span={8} offset={4}>
                    {/* 修改长期医嘱 */}
                    <Row>
                        <Col span={24} style={{ height: "30px", display: "flex" }}>
                            <div style={{ height: "100%", width: "10px", backgroundColor: "rgb(31, 109, 226)" }}></div>
                            <div style={{ height: "100%", lineHeight: "30px", marginLeft: "20px" }}><h1 style={{ fontWeight: 700 }}>修改长期医嘱</h1></div>
                        </Col>
                    </Row>
                    {/* 长期医嘱 */}
                    <Row>
                        <Col>
                            <Form form={changqi} onFinish={changqiYiZhu}>
                                <FormItem label="住院号" style={{ marginTop: "20px" }} name="zhuYuanHao">
                                    <Input disabled></Input>
                                </FormItem>
                                <FormItem label="医嘱时效" style={{ marginBottom: "20px" }} name="time">
                                    <RangePicker showTime
                                    // ={{
                                    //     hideDisabledOptions: true
                                    // }}
                                        format="YYYY-MM-DD HH:mm:ss"></RangePicker>
                                </FormItem>
                                <FormItem label="用药时间" name="yongYaoShiJian">
                                    <Input></Input>
                                </FormItem>
                                <FormItem label="药品名称" name="yaoPinMingCheng">
                                    <Input></Input>
                                </FormItem>
                                <FormItem label="药品用法" name="yaoPinYongFa">
                                    <Input></Input>
                                </FormItem>
                                <FormItem label="药品用量" name="yaoPinYongLiang">
                                    <Input type="number"></Input>
                                </FormItem>
                                <FormItem label="科室信息" name="keShiDaiMa">
                                    <Select options={kesOption} style={{ width: "120px" }} onChange={cqKeShj}></Select>
                                </FormItem>
                                <FormItem label="所属医生" name="yiShengBianMa">
                                    <Select options={ysOption} style={{ width: "120px" }} onChange={cqYiSheng}></Select>
                                </FormItem>
                                <FormItem label="是否出院" name="chuYuanFou">
                                    <Select options={chuyuanfou} style={{ width: "120px" }}></Select>
                                </FormItem>
                                <FormItem style={{ textAlign: "center" }}>
                                    <Button type="primary" htmlType="submit">保存修改</Button>
                                </FormItem>
                            </Form>
                        </Col>
                    </Row>
                </Col>
                <Col span={8} offset={4}>
                    {/* 修改临时医嘱 */}
                    <Row>
                        <Col span={24} style={{ height: "30px", display: "flex" }}>
                            <div style={{ height: "100%", width: "10px", backgroundColor: "rgb(31, 109, 226)" }}></div>
                            <div style={{ height: "100%", lineHeight: "30px", marginLeft: "20px" }}><h1 style={{ fontWeight: 700 }}>修改临时医嘱</h1></div>
                        </Col>
                    </Row>
                    {/* 临时医嘱 */}
                    <Row>
                        <Col>
                            <Form form={lingshi} onFinish={lingshiYiZhu}>
                                <FormItem label="住院号" style={{ marginTop: "20px" }} name="zhuYuanHao">
                                    <Input disabled></Input>
                                </FormItem>
                                <FormItem label="医嘱日期" style={{ marginBottom: "20px" }} name="yiZhuRiQi">
                                    <DatePicker showTime  format="YYYY-MM-DD HH:mm:ss"></DatePicker>
                                </FormItem>
                                <FormItem label="用药时间" name="yongYaoShiJian">
                                    <Input></Input>
                                </FormItem>
                                <FormItem label="药品名称" name="yaoPinMingCheng">
                                    <Input></Input>
                                </FormItem>
                                <FormItem label="药品用法" name="yaoPinYongFa">
                                    <Input></Input>
                                </FormItem>
                                <FormItem label="药品用量" name="yaoPinYongLiang">
                                    <Input type="number"></Input>
                                </FormItem>
                                <FormItem label="科室信息" name="keShiDaiMa">
                                    <Select options={lskesOption} style={{ width: "120px" }} onChange={lsKeShi}></Select>
                                </FormItem>
                                <FormItem label="所属医生" name="yiShengDaiMa">
                                    <Select options={lsysOption} style={{ width: "120px" }} onChange={lsYiSheng}></Select>
                                </FormItem>
                                <FormItem label="是否出院" name="chuYuanFou">
                                    <Select options={chuyuanfou} style={{ width: "120px" }}></Select>
                                </FormItem>
                                <FormItem style={{ textAlign: "center" }}>
                                    <Button type="primary" htmlType="submit">保存修改</Button>
                                </FormItem>
                            </Form>
                        </Col>
                    </Row>
                </Col>
            </Row>
            <Row style={{ marginTop: "60px" }}>
                <Col span={24} style={{ textAlign: "center" }}>
                    <Button type="primary" onClick={goback}>返回</Button>
                </Col>
            </Row>
        </div>
    );
}

export default XiuGaiYiZhu;
